//
//  ZCdetectiondetailsVC.m
//  zctx
//
//  Created by apple on 16/1/4.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCdetectiondetailsVC.h"
#import "ZCdetectionCell.h"
#import "ZCdetectionPostCell.h"
#import "WyzAlbumViewController.h"
#import "ZCLoginVC.h"
@interface ZCdetectiondetailsVC ()<UITableViewDataSource,UITableViewDelegate,ZCdetectionPostCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property BOOL isshow;
@end

@implementation ZCdetectiondetailsVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCdetectiondetailsVC"];
    [MTA trackPageViewBegin:@"ZCdetectiondetailsVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCdetectiondetailsVC"];
    [MTA trackPageViewEnd:@"ZCdetectiondetailsVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"检测详情";
    [self initnavegation];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self initrequst];
    });
    
    if ([self.projectModel.result isEqualToString:@"0"]) {
        self.isshow = NO;
    }else{
        self.isshow = YES;
    }
    self.dataArray = [[NSMutableArray alloc]init];
    [self.view addSubview:self.tableView];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCdetectionCell" bundle:nil] forCellReuseIdentifier:@"detectioncell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCdetectionPostCell" bundle:nil] forCellReuseIdentifier:@"postcell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
}

-(void)initrequst{
    
    [NetworkManager requestFoundingGetOnepackagesWithId:self.pid Mid:UserModel.mid password:UserModel.password success:^(NSArray *dictResponse) {
        if (dictResponse.count>0) {
            self.dataArray= [dictResponse copy];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.isshow ==YES) {
        return 5+self.dataArray.count;
    }else{
        return 4 + self.dataArray.count;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isshow ==YES) {
        if (section==0) {
            return 2;
        }else if (section==1) {
            return 2;
        }else if (section==2){
            return 2;
        }else if (section==3){
            if (self.isshow ==YES) {
                return 3;
            }else{
                return 2;
            }
        }else if(section==4){
            return 1;
        }else{
            return 3;
        }
    }else{
        if (section==0) {
            return 2;
        }else if (section==1){
            return 2;
        }else if (section==2){
            if (self.isshow ==YES) {
                return 3;
            }else{
                return 2;
            }
        }else if(section==3){
            return 1;
        }else{
            return 3;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *IDcell = @"detectioncell";
    ZCdetectionCell *cell = [tableView dequeueReusableCellWithIdentifier:IDcell];
    if (cell == nil) {
        cell = [[ZCdetectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDcell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.isshow ==YES) {
        if (indexPath.section==0) {
            if (indexPath.row==0) {
                cell.contentLable.text = @"检测结果";
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
            }else{
                cell.contentLable.textAlignment = NSTextAlignmentLeft;
                if ([self.projectModel.result isEqualToString:@"1"]) {
                    cell.contentLable.text = @"合格";
                    cell.contentLable.textColor = MainColor;
                }else{
                    cell.contentLable.text = self.test_result;
                    cell.contentLable.textColor = [UIColor redColor];
                }
                
            }
        }else if (indexPath.section==1) {
            if (indexPath.row==0) {
                cell.contentLable.text = @"抽样地";
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
            }else{
                cell.contentLable.textAlignment = NSTextAlignmentLeft;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                cell.contentLable.text = self.projectModel.location;
            }
        }else if (indexPath.section ==2){
            if (indexPath.row==0) {
                cell.contentLable.text = @"检测机构";
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
            }else{
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                cell.contentLable.text = self.projectModel.institution;
            }
        }else if (indexPath.section ==3){
            if (self.isshow ==YES) {
                if (indexPath.row==0) {
                    cell.contentLable.text = @"检测内容";
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
                }else if(indexPath.row==1){
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                    cell.contentLable.text = self.projectModel.packagename;
                }else{
                    ZCdetectionPostCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postcell"];
                    if (cell == nil) {
                        cell = [[ZCdetectionPostCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDcell];
                        
                    }
                    if (self.dataArray.count>0) {
                        cell.model = self.projectModel;
                    }
                    cell.delegate = self;
                    return cell;
                }
            }else{
                if (indexPath.row==0) {
                    cell.contentLable.text = @"检测内容";
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
                }else{
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                    cell.contentLable.text = self.projectModel.packagename;
                }
            }
        }else if(indexPath.section==4){
            cell.contentLable.text = @"危害简介";
            cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
        }else{
            NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section-5];
            if (indexPath.row==0) {
                NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
                cell.contentLable.textAlignment = NSTextAlignmentCenter;
                cell.contentLable.text = title;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
            }else if(indexPath.row==1){
                NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
                cell.contentLable.text = content;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
            }else{
                NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];
                cell.contentLable.text = information;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
            }
        }
    }else{
        if (indexPath.section==0) {
            if (indexPath.row==0) {
                cell.contentLable.text = @"抽样地";
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
            }else{
                cell.contentLable.textAlignment = NSTextAlignmentLeft;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                cell.contentLable.text = self.projectModel.location;
            }
        }else if (indexPath.section ==1){
            if (indexPath.row==0) {
                cell.contentLable.text = @"检测机构";
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
            }else{
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                cell.contentLable.text = self.projectModel.institution;
            }
        }else if (indexPath.section ==2){
            if (self.isshow ==YES) {
                if (indexPath.row==0) {
                    cell.contentLable.text = @"检测内容";
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
                }else if(indexPath.row==1){
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                    cell.contentLable.text = self.projectModel.packagename;
                }else{
                    ZCdetectionPostCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postcell"];
                    if (cell == nil) {
                        cell = [[ZCdetectionPostCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDcell];
                        
                    }
                    if (self.dataArray.count>0) {
                        cell.model = self.projectModel;
                    }
                    cell.delegate = self;
                    return cell;
                }
            }else{
                if (indexPath.row==0) {
                    cell.contentLable.text = @"检测内容";
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
                }else{
                    cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
                    cell.contentLable.text = self.projectModel.packagename;
                }
            }
        }else if(indexPath.section==3){
            cell.contentLable.text = @"危害简介";
            cell.contentLable.textColor = [UIColor colorWithHexString:@"#525252"];
        }else{
            NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section-4];
            if (indexPath.row==0) {
                NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
                cell.contentLable.textAlignment = NSTextAlignmentCenter;
                cell.contentLable.text = title;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#000000"];
            }else if(indexPath.row==1){
                NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
                cell.contentLable.text = content;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
            }else{
                NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];
                cell.contentLable.text = information;
                cell.contentLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
            }
        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier = @"detectioncell";
    ZCdetectionCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (self.isshow ==YES) {
        if (indexPath.section==3) {
            if (indexPath.row==0) {
                return 44;
            }else if (indexPath.row==1){
                [cell.contentLable setText:self.projectModel.packagename];
                [cell setNeedsUpdateConstraints];
                [cell updateConstraintsIfNeeded];
                // 得到cell的contentView需要的真实高度
                CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
                height += 1.0f;
                return height;
            }else{
                return 300;
            }
        }else if (indexPath.section>4) {
            if (self.dataArray) {
                NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section-5];
                NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
                NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
                NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];
                if (indexPath.row==0) {
                    [cell.contentLable setText:title];
                }else if (indexPath.row==1){
                    [cell.contentLable setText:content];
                }else{
                    [cell.contentLable setText:information];
                }
                [cell setNeedsUpdateConstraints];
                [cell updateConstraintsIfNeeded];
                // 得到cell的contentView需要的真实高度
                CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
                height += 1.0f;
                return height;
            }else{
                return 44;
            }
        }else{
            return 44;
        }

    }else{
        if (indexPath.section==2) {
            if (indexPath.row==0) {
                return 44;
            }else if (indexPath.row==1){
                [cell.contentLable setText:self.projectModel.packagename];
                [cell setNeedsUpdateConstraints];
                [cell updateConstraintsIfNeeded];
                // 得到cell的contentView需要的真实高度
                CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
                height += 1.0f;
                return height;
            }else{
                return 300;
            }
        }else if (indexPath.section>3) {
            if (self.dataArray) {
                NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section-4];
                NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
                NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
                NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];
                if (indexPath.row==0) {
                    [cell.contentLable setText:title];
                }else if (indexPath.row==1){
                    [cell.contentLable setText:content];
                }else{
                    [cell.contentLable setText:information];
                }
                [cell setNeedsUpdateConstraints];
                [cell updateConstraintsIfNeeded];
                // 得到cell的contentView需要的真实高度
                CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
                height += 1.0f;
                return height;
            }else{
                return 44;
            }
        }else{
            return 44;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (self.isshow ==YES) {
        if (section==0 ||section==4) {
            return 0.0001;
        }else{
            return 10;
        }
    }else{
        if (section==0 ||section==3) {
            return 0.0001;
        }else{
            return 10;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = RGB(245, 245, 245);
        _tableView.separatorColor = RGB(226, 226, 226);
    }
    return _tableView;
}

-(void)ClickImgViewZCdetection:(NSMutableArray *)imgArray{
    NSLog(@"11 = %@",imgArray);
    
    if (UserModel.password.length>1) {
        if (imgArray.count>0) {
            WyzAlbumViewController *wyzAlbumVC = [[WyzAlbumViewController alloc]init];
            wyzAlbumVC.currentIndex = 0;
            wyzAlbumVC.imgArr = imgArray;
            [self.navigationController pushViewController:wyzAlbumVC animated:YES];
        }
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
